<?php
/**
 * Notes:
 * Created by PhpStorm.
 * Author: TS
 * Date: 2019/10/4
 * Time: 13:37
 */

namespace app\index\controller;


use app\model\FriendGroup;
use app\model\Users;
use app\validate\UsersValidate;
use think\Session;
class User
{
    /**
     * 用户登记
     * @return false|string
     */
    public function signIn(){
        if(request()->isPost()) {
            $user_data = request()->only('user_pwd,user_name,user_phone,user_email,user_sex,user_birthday,user_header,user_info,user_address,department_id,role_id');
            $time = time();
            $user_data['registe_time'] = time();
            $user_data['user_ip'] = request()->ip();
            $user_data['user_status'] = 1;
            $UserValidate = new UsersValidate();
            //验证参数
            if(!$UserValidate->check($user_data)) {return res(0,$UserValidate->getError());}
            $user_data['user_pwd'] = md5($user_data['user_pwd']);
            $user = new Users();
            $res = $user->signIn($user_data);
            //登记成功
            if ($res) {
                //添加默认分组
                $friendgroup = new FriendGroup();
                $res = json_decode($res,true);
                $friendgroup->addDefault($res["user_id"],$time);
                return res('1', '登记成功',$res);
            }
            return res(0, $user->getError());
        }
        return res(0,'请求方式错误');
    }

    /**
     * 用户登录
     * @return false|string
     */
    public function signOn(){
        if(request()->isPost() || request()->isGet()){
            $user_data = request()->only('user_id,user_pwd');
            $UserValidate = new UsersValidate();
            //验证字符
            if(!$UserValidate->scene('signon')->check($user_data)){
                return res(0,$UserValidate->getError());
            }
            $user_data['user_pwd'] = md5($user_data['user_pwd']);
            $user = new Users();
            $res = $user->signOn($user_data);
            if($res){
                //登录成功，更新IP
                $ip = ['user_id' => request()->ip()];
                $user->edit($res['user_id'],$ip);
                $res_data = $res->hidden(['user_pwd']);
                $birthday = get_constellation($res['user_birthday']);
                $res_data["user_age"] = $birthday["age"];
                $res_data["user_animals"] = $birthday["animals"];
                $res_data["user_constellation"] = $birthday["constellation"];
                session('contact_user', $res_data);
                return res(1,'登录成功',$res_data);
            }
            return res(0,'登录失败');
        }
        return res(0,'请求方式错误');
    }

    public function checkLogin(){
            $users = session("contact_user");
            if($users == null){
                return res(0,'未登录');
            }else{
                return res(1,'已登录',$users);
            }
    }

    public function layout(){
        $res = session("contact_user",null);
        if(session("contact_user") == null){
            return res(1,'登出成功');
        }
        return res(0,'登出失败');
    }


    /**
     * 动态修改用户数据
     * @return false|string
     */
    public function edit(){
        if(request()->isPost()) {
            //过滤接收参数
            $user_data = request()->only('user_id,user_pwd,user_name,user_phone,user_email,user_sex,user_birthday,user_header,user_info,user_address,department_id,role_id');
            //判断是否存在用户标识
            if(isset($user_data['user_id'])) {
                $id = $user_data['user_id'];
                //更新集中移除用户标识
                unset($user_data['user_id']);
                if(isset($user_data['user_birthday'])) $user_data['user_birthday'] = strtotime($user_data['user_birthday']);
                $user = new Users();
                $res = $user->edit($id, $user_data);
                if ($res) {
                    $user_res = $user->getUserById($id);
                    return res(1, '修改成功',$user_res);
                }
                return res(0, '修改失败');
            }
            return res(0,'缺少用户标识');
        }
        return res(0,'请求方式错误');
    }


    public function getUserById($id){
        if(request()->isPost()){
            $user = new Users();
            $user = $user->getUserById($id);
            if($user) {
                return res(1, '查找成功', $user);
            }
            return res(0,'无数据');
        }
        return res(0,'请求方式错误');
    }

    public function getUsersByKey(){
        if (request()->isPost()){
            $key = input('post.search_key');
            $UserModel = new Users();
            $res_data = $UserModel->searchUsers($key);
            return res(1,'',$res_data);
        }
        return res(0,MSG_NOT_POST);
    }

}